function login() {
  $.ajax({
    type: 'POST',
    url: '/login',
    data: $("#signInForm").serialize(),
    success: function(data) {
      var JsonObj = $.parseJSON(data); 
      if (JsonObj['error']) {
        showMessage('#signInForm', JsonObj)
      } else if (JsonObj['success']) {
        window.location = '/profile';
      }
    },
  }); 
}

function logout() {
  $.ajax({
    url: '/logout',
    success: function(data) {  window.location = '/profile';},
  });
}

function createAccount() {
  $.ajax({
    type: 'POST',
    url: '/createAccount',
    data: $("#CreateAccountForm").serialize(),
    success: function(data) {
      var JsonObj = $.parseJSON(data);
      showMessage('#CreateAccountTitle', JsonObj);
      if (JsonObj['success']) {
        $('#CreateAccountForm').hide();
      }
    },
  });
}



function buyDeal() {
  $.post('/buyDeal',
    function(data) {
      var JsonObj = $.parseJSON(data);
      if (JsonObj['error']) {
      } else {
        updateViews($.parseJSON(JsonObj['data']));
      }
  });
}

$(document).ready(function () {
  $('#closeDialogButton, #dialog-overlay').click(function () {     
      $('#dialog-overlay, #dialog-box').hide();       
      return false;
  });
  $(window).resize(function () {
      if (!$('#dialog-box').is(':hidden')) popup();       
  }); 
  

  
  $('#checkoutButton').click(function() {
    
    var idPrice = getIdPrice();
    $.ajax({
      url: '/paiement/getToken?cmd=openTrx&idPrice=' + idPrice, // A call to server side to initiate the payment process
      dataType: 'html',
      success: function(response) {
        eval(response);
        var path = "https://" + window.location.host;
        zoozCheckout(data.token, 
            path + "/profile",
            path + "/profile?trx=failed", 
            "OhMyBars", 
            true); // Environment flag: sandbox = true, production = false 
      }
    });
    
  });
});


function getIdPrice() {
  var text = $('#nbCouponsSelect .ui-btn-text').text();
  var idPrice = -1;
  $('#selectChoice0 option').each(function() {
    if ($(this).text() == text) {
      idPrice = $(this).val();
    }
  })
  return idPrice;
}


function popup(message) {
  $('#dialog-box').width($('#slider').width() * 0.9);
  var maskHeight = $(document).height();
  var maskWidth = $(window).width();
  var dialogTop =  (maskHeight/3) - ($('#dialog-box').height());  
  var dialogLeft = (maskWidth/2) - ($('#dialog-box').width()/2); 
  $('#dialog-overlay').css({height:maskHeight, width:maskWidth}).show();
  $('#dialog-box').css({top:dialogTop, left:dialogLeft}).show();
  $('#dialog-message').html(message);
}


function getACouponPopup() {
  popup($("#getacouponTemplate").html());
  $("#dialog-box").trigger('create');
}

function showMessage(selectorParent, JsonObj) {
  deleteMessages();
  var className = (JsonObj['success']) ? 'successMsg' : 'errorMsg';
  $(selectorParent).after(
      $('<div></div>').addClass(className).html(JsonObj['msg']));
}

function updateDealBoughtView(data) {
  $('#updateDealBoughtView').html('=> You have ' + data['dealBoughtCount'] + ' cupons bought.');
}


